package com.yjt.sousou.detail;

import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yjt.sousou.R;
import com.yjt.sousou.base.BaseActivity;
import com.yjt.sousou.detail.entity.OrderDetail;

import java.text.DecimalFormat;
import java.util.ArrayList;

import butterknife.BindView;

/**
 * 作者：83734
 * 时间：2018/12/8
 * 描述：
 */
public class PartsListActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_total)
    TextView mTvTotal;
    @BindView(R.id.rl_list)
    RecyclerView mRecyclerView;
    private BaseQuickAdapter mAdapter;
    @Override
    protected Object setContentLayout() {
        return R.layout.activity_parts_list;
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        mTvTitle.setText("配件列表");
        ArrayList<OrderDetail.DataBean.PeiJianBean> parts = getIntent().getParcelableArrayListExtra("parts");
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(manager);

        mAdapter = new BaseQuickAdapter<OrderDetail.DataBean.PeiJianBean,BaseViewHolder>(R.layout.item_parts_list,parts) {
            @Override
            protected void convert(BaseViewHolder helper, OrderDetail.DataBean.PeiJianBean item) {
               helper.setText(R.id.tv_num,String.valueOf(helper.getAdapterPosition()+1));
               helper.setText(R.id.tv_parts,item.getProname());
               helper.setText(R.id.tv_standard,item.getXinghao());
               helper.setText(R.id.tv_price,item.getDanjia());
               helper.setText(R.id.tv_parts_num,item.getProduct_num());
            }
        };
        mRecyclerView.setAdapter(mAdapter);

        mTvTotal.setText(String.valueOf(getTotal(parts)));
    }

    private double getTotal(ArrayList<OrderDetail.DataBean.PeiJianBean> parts){
        double total = 0;
        for (OrderDetail.DataBean.PeiJianBean entity : parts) {
            DecimalFormat df = new DecimalFormat("#.00");
            String str = df.format(Double.parseDouble(entity.getDanjia()));
            double itemTotal = Integer.parseInt(entity.getProduct_num())*Double.parseDouble(str);
            total+=itemTotal;
        }
        return total;
    }
}
